home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / (gcc-1.37.π) / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  59.9 KB  |  2,099 lines  |  [TEXT/KAHL]

  1. /* Language-independent node constructors for parse phase of GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Copyright (C) 1989, 1990 Apple Computer, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the low level primitives for operating on tree nodes,
  23.    including allocation, list operations, interning of identifiers,
  24.    construction of data type nodes and statement nodes,
  25.    and construction of type conversion nodes.  It also contains
  26.    tables index by tree code that describe how to take apart
  27.    nodes of that code.
  28.  
  29.    It is intended to be language-independent, but occasionally
  30.    calls language-dependent routines defined (for C) in typecheck.c.
  31.  
  32.    The low-level allocation routines oballoc and permalloc
  33.    are used also for allocating many other kinds of objects
  34.    by all passes of the compiler.  */
  35.  
  36. #include "config.h"
  37. #include <stdio.h>
  38. #include "tree.h"
  39. #include "obstack.h"
  40. #include "gvarargs.h"
  41. #include "flags.h"
  42.  
  43. #define obstack_chunk_alloc xmalloc
  44. #define obstack_chunk_free free
  45.  
  46. extern int xmalloc ();
  47. extern void free ();
  48.  
  49. /* Tree nodes of permanent duration are allocated in this obstack.
  50.    They are the identifier nodes, and everything outside of
  51.    the bodies and parameters of function definitions.  */
  52.  
  53. struct obstack permanent_obstack;
  54.  
  55. /* The initial RTL, and all ..._TYPE nodes, in a function
  56.    are allocated in this obstack.  Usually they are freed at the
  57.    end of the function, but if the function is inline they are saved.  */
  58.  
  59. struct obstack maybepermanent_obstack;
  60.  
  61. /* The contents of the current function definition are allocated
  62.    in this obstack, and all are freed at the end of the function.  */
  63.  
  64. struct obstack temporary_obstack;
  65.  
  66. /* The tree nodes of an expression are allocated
  67.    in this obstack, and all are freed at the end of the expression.  */
  68.  
  69. struct obstack momentary_obstack;
  70.  
  71. /* This points at either permanent_obstack or maybepermanent_obstack.  */
  72.  
  73. struct obstack *saveable_obstack;
  74.  
  75. /* This is same as saveable_obstack during parse and expansion phase;
  76.    it points to temporary_obstack during optimization.
  77.    This is the obstack to be used for creating rtl objects.  */
  78.  
  79. struct obstack *rtl_obstack;
  80.  
  81. /* This points at either permanent_obstack or temporary_obstack.  */
  82.  
  83. struct obstack *current_obstack;
  84.  
  85. /* This points at either permanent_obstack or temporary_obstack
  86.    or momentary_obstack.  */
  87.  
  88. struct obstack *expression_obstack;
  89.  
  90. /* Addresses of first objects in some obstacks.
  91.    This is for freeing their entire contents.  */
  92. char *maybepermanent_firstobj;
  93. char *temporary_firstobj;
  94. char *momentary_firstobj;
  95.  
  96. /* Nonzero means all ..._TYPE nodes should be allocated permanently.  */
  97.  
  98. int all_types_permanent;
  99.  
  100. /* Stack of places to restore the momentary obstack back to.  */
  101.    
  102. struct momentary_level
  103. {
  104.   /* Pointer back to previous such level.  */
  105.   struct momentary_level *prev;
  106.   /* First object allocated within this level.  */
  107.   char *base;
  108.   /* Value of expression_obstack saved at entry to this level.  */
  109.   struct obstack *obstack;
  110. };
  111.  
  112. struct momentary_level *momentary_stack;
  113.  
  114. /* Table indexed by tree code giving a string containing a character
  115.    classifying the tree code.  Possibilities are
  116.    t, d, s, c, r and e.  See tree.def for details.  */
  117.  
  118. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  119.  
  120. char *tree_code_type[] = {
  121. #include "tree.def"
  122. };
  123. #undef DEFTREECODE
  124.  
  125. /* Table indexed by tree code giving number of expression
  126.    operands beyond the fixed part of the node structure.
  127.    Not used for types or decls.  */
  128.  
  129. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  130.  
  131. int tree_code_length[] = {
  132. #include "tree.def"
  133. };
  134. #undef DEFTREECODE
  135.  
  136. /* Counter for assigning unique ids to all tree nodes.  */
  137.  
  138. int tree_node_counter = 0;
  139.  
  140. /* Hash table for uniquizing IDENTIFIER_NODEs by name.  */
  141.  
  142. #define MAX_HASH_TABLE 1009
  143. #ifdef MPW
  144. /* Save some space by dynamically allocating the hash table. */
  145. static tree *hash_table;
  146. #else
  147. static tree hash_table[MAX_HASH_TABLE];    /* id hash buckets */
  148. #endif /* MPW */
  149.  
  150. /* 0 while creating built-in identifiers.  */
  151. static int do_identifier_warnings;
  152.  
  153. /* Init data for node creation, at the beginning of compilation.  */
  154.  
  155. void
  156. init_tree ()
  157. {
  158.   obstack_init (&permanent_obstack);
  159.  
  160.   obstack_init (&temporary_obstack);
  161.   temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
  162.   obstack_init (&momentary_obstack);
  163.   momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
  164.   obstack_init (&maybepermanent_obstack);
  165.   maybepermanent_firstobj
  166.     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  167.  
  168.   current_obstack = &permanent_obstack;
  169.   expression_obstack = &permanent_obstack;
  170.   rtl_obstack = saveable_obstack = &permanent_obstack;
  171.   tree_node_counter = 1;
  172. #ifdef MPW
  173.   /* Allocate the hash table. */
  174.   hash_table = (tree *) xmalloc((sizeof( tree)) * MAX_HASH_TABLE);
  175.   bzero (hash_table, (sizeof(tree)) * MAX_HASH_TABLE);
  176. #else
  177.   bzero (hash_table, sizeof hash_table);
  178. #endif /* MPW */
  179. }
  180.  
  181. /* Start allocating on the temporary (per function) obstack.
  182.    This is done in start_function before parsing the function body,
  183.    and before each initialization at top level, and to go back
  184.    to temporary allocation after doing end_temporary_allocation.  */
  185.  
  186. void
  187. temporary_allocation ()
  188. {
  189.   current_obstack = &temporary_obstack;
  190.   expression_obstack = &temporary_obstack;
  191.   rtl_obstack = saveable_obstack = &maybepermanent_obstack;
  192.   momentary_stack = 0;
  193. }
  194.  
  195. /* Start allocating on the permanent obstack but don't
  196.    free the temporary data.  After calling this, call
  197.    `permanent_allocation' to fully resume permanent allocation status.  */
  198.  
  199. void
  200. end_temporary_allocation ()
  201. {
  202.   current_obstack = &permanent_obstack;
  203.   expression_obstack = &permanent_obstack;
  204.   rtl_obstack = saveable_obstack = &permanent_obstack;
  205. }
  206.  
  207. /* Resume allocating on the temporary obstack, undoing
  208.    effects of `end_temporary_allocation'.  */
  209.  
  210. void
  211. resume_temporary_allocation ()
  212. {
  213.   current_obstack = &temporary_obstack;
  214.   expression_obstack = &temporary_obstack;
  215.   rtl_obstack = saveable_obstack = &maybepermanent_obstack;
  216. }
  217.  
  218. /* Nonzero if temporary allocation is currently in effect.
  219.    Zero if currently doing permanent allocation.  */
  220.  
  221. int
  222. allocation_temporary_p ()
  223. {
  224.   return current_obstack == &temporary_obstack;
  225. }
  226.  
  227. /* Go back to allocating on the permanent obstack
  228.    and free everything in the temporary obstack.
  229.    This is done in finish_function after fully compiling a function.  */
  230.  
  231. void
  232. permanent_allocation ()
  233. {
  234.   /* Free up previous temporary obstack data */
  235.   obstack_free (&temporary_obstack, temporary_firstobj);
  236.   obstack_free (&momentary_obstack, momentary_firstobj);
  237.   obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
  238.  
  239.   current_obstack = &permanent_obstack;
  240.   expression_obstack = &permanent_obstack;
  241.   rtl_obstack = saveable_obstack = &permanent_obstack;
  242. }
  243.  
  244. /* Save permanently everything on the maybepermanent_obstack.  */
  245.  
  246. void
  247. preserve_data ()
  248. {
  249.   maybepermanent_firstobj
  250.     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  251. }
  252.  
  253. /* Allocate SIZE bytes in the current obstack
  254.    and return a pointer to them.
  255.    In practice the current obstack is always the temporary one.  */
  256.  
  257. char *
  258. oballoc (size)
  259.      int size;
  260. {
  261.   return (char *) obstack_alloc (current_obstack, size);
  262. }
  263.  
  264. /* Free the object PTR in the current obstack
  265.    as well as everything allocated since PTR.
  266.    In practice the current obstack is always the temporary one.  */
  267.  
  268. void
  269. obfree (ptr)
  270.      char *ptr;
  271. {
  272. #ifdef MPW_C
  273.   /* This doesn't make any sense, but the MPW C RA is happier... */
  274.   int tmp;
  275.     
  276.   current_obstack->temp = ptr - (char *) current_obstack->chunk;
  277.   if (current_obstack->temp >= 0) {
  278.       tmp = current_obstack->chunk_limit - ((char *) current_obstack->chunk);
  279.       if (current_obstack->temp < tmp) {
  280.       current_obstack->object_base =
  281.         current_obstack->temp + (char *) current_obstack->chunk;
  282.       current_obstack->next_free = current_obstack->object_base;
  283.       return;
  284.       }
  285.   }
  286.   _obstack_free (current_obstack,
  287.          current_obstack->temp + (char *) current_obstack->chunk);
  288. #else /* what this is supposed to be */
  289.   obstack_free (current_obstack, ptr);
  290. #endif /* MPW_C */
  291. }
  292.  
  293. /* Allocate SIZE bytes in the permanent obstack
  294.    and return a pointer to them.  */
  295.  
  296. char *
  297. permalloc (size)
  298.      long size;
  299. {
  300.   return (char *) obstack_alloc (&permanent_obstack, size);
  301. }
  302.  
  303. /* Allocate SIZE bytes in the saveable obstack
  304.    and return a pointer to them.  */
  305.  
  306. char *
  307. savealloc (size)
  308.      int size;
  309. {
  310.   return (char *) obstack_alloc (saveable_obstack, size);
  311. }
  312.  
  313. /* Start a level of momentary allocation.
  314.    In C, each compound statement has its own level
  315.    and that level is freed at the end of each statement.
  316.    All expression nodes are allocated in the momentary allocation level.  */
  317.  
  318. void
  319. push_momentary ()
  320. {
  321.   struct momentary_level *tem
  322.     = (struct momentary_level *) obstack_alloc (&momentary_obstack,
  323.                         sizeof (struct momentary_level));
  324.   tem->prev = momentary_stack;
  325.   tem->base = (char *) obstack_base (&momentary_obstack);
  326.   tem->obstack = expression_obstack;
  327.   momentary_stack = tem;
  328.   expression_obstack = &momentary_obstack;
  329. }
  330.  
  331. /* Free all the storage in the current momentary-allocation level.
  332.    In C, this happens at the end of each statement.  */
  333.  
  334. void
  335. clear_momentary ()
  336. {
  337.   obstack_free (&momentary_obstack, momentary_stack->base);
  338. }
  339.  
  340. /* Discard a level of momentary allocation.
  341.    In C, this happens at the end of each compound statement.
  342.    Restore the status of expression node allocation
  343.    that was in effect before this level was created.  */
  344.  
  345. void
  346. pop_momentary ()
  347. {
  348.   struct momentary_level *tem = momentary_stack;
  349.   momentary_stack = tem->prev;
  350.   obstack_free (&momentary_obstack, tem);
  351.   expression_obstack = tem->obstack;
  352. }
  353.  
  354. /* Call when starting to parse a declaration:
  355.    make expressions in the declaration last the length of the function.
  356.    Returns an argument that should be passed to resume_momentary later.  */
  357.  
  358. int
  359. suspend_momentary ()
  360. {
  361.   register int tem = expression_obstack == &momentary_obstack;
  362.   expression_obstack = saveable_obstack;
  363.   return tem;
  364. }
  365.  
  366. /* Call when finished parsing a declaration:
  367.    restore the treatment of node-allocation that was
  368.    in effect before the suspension.
  369.    YES should be the value previously returned by suspend_momentary.  */
  370.  
  371. void
  372. resume_momentary (yes)
  373.      int yes;
  374. {
  375.   if (yes)
  376.     expression_obstack = &momentary_obstack;
  377. }
  378.  
  379. /* Return a newly allocated node of code CODE.
  380.    Initialize the node's unique id and its TREE_PERMANENT flag.
  381.    For decl and type nodes, some other fields are initialized.
  382.    The rest of the node is initialized to zero.
  383.  
  384.    Achoo!  I got a code in the node.  */
  385.  
  386. tree
  387. make_node (code)
  388.      enum tree_code code;
  389. {
  390.   register tree t;
  391.   register int type = *tree_code_type[(int) code];
  392.   register int length;
  393.   register struct obstack *obstack = current_obstack;
  394.   register int i;
  395.  
  396.   switch (type)
  397.     {
  398.     case 'd':  /* A decl node */
  399.       length = sizeof (struct tree_decl);
  400.       /* All decls in an inline function need to be saved.  */
  401.       if (obstack != &permanent_obstack)
  402.     obstack = saveable_obstack;
  403.       /* PARM_DECLs always go on saveable_obstack, not permanent,
  404.      even though we may make them before the function turns
  405.      on temporary allocation.  */
  406.       else if (code == PARM_DECL)
  407.     obstack = &maybepermanent_obstack;
  408.       break;
  409.  
  410.     case 't':  /* a type node */
  411.       length = sizeof (struct tree_type);
  412.       /* All data types are put where we can preserve them if nec.  */
  413.       if (obstack != &permanent_obstack)
  414.     obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
  415.       break;
  416.  
  417.     case 's':  /* a stmt node */
  418.       length = sizeof (struct tree_common)
  419.     + 2 * sizeof (int)
  420.       + tree_code_length[(int) code] * sizeof (char *);
  421.       /* All stmts are put where we can preserve them if nec.  */
  422.       if (obstack != &permanent_obstack)
  423.     obstack = saveable_obstack;
  424.       break;
  425.  
  426.     case 'r':  /* a reference */
  427.     case 'e':  /* an expression */
  428.       obstack = expression_obstack;
  429.       length = sizeof (struct tree_exp)
  430.     + (tree_code_length[(int) code] - 1) * sizeof (char *);
  431.       break;
  432.  
  433.     case 'c':  /* a constant */
  434.       obstack = expression_obstack;
  435.       /* We can't use tree_code_length for this, since the number of words
  436.      is machine-dependent due to varying alignment of `double'.  */
  437.       if (code == REAL_CST)
  438.     {
  439.       length = sizeof (struct tree_real_cst);
  440.       break;
  441.     }
  442.  
  443.     case 'x':  /* something random, like an identifier.  */
  444.       length = sizeof (struct tree_common)
  445.     + tree_code_length[(int) code] * sizeof (char *);
  446.       /* Identifier nodes are always permanent since they are
  447.      unique in a compiler run.  */
  448.       if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
  449.     }
  450.  
  451.   t = (tree) obstack_alloc (obstack, length);
  452.  
  453.   TREE_UID (t) = tree_node_counter++;
  454.   TREE_TYPE (t) = 0;
  455.   TREE_CHAIN (t) = 0;
  456.   for (i = (length / sizeof (int)) - 1;
  457.        i >= sizeof (struct tree_common) / sizeof (int) - 1;
  458.        i--)
  459.     ((int *) t)[i] = 0;
  460.  
  461.   TREE_SET_CODE (t, code);
  462.   if (obstack == &permanent_obstack)
  463.     TREE_PERMANENT (t) = 1;
  464.  
  465.   if (type == 'd')
  466.     {
  467.       extern int lineno;
  468.  
  469.       DECL_ALIGN (t) = 1;
  470.       DECL_SIZE_UNIT (t) = 1;
  471.       DECL_VOFFSET_UNIT (t) = 1;
  472.       DECL_SOURCE_LINE (t) = lineno;
  473.       DECL_SOURCE_FILE (t) = input_filename;
  474.       DECL_PARAM(t) = NULL;
  475.     }
  476.  
  477.   if (type == 't')
  478.     {
  479.       TYPE_ALIGN (t) = 1;
  480.       TYPE_SIZE_UNIT (t) = 1;
  481.       TYPE_MAIN_VARIANT (t) = t;
  482.     }
  483.  
  484.   if (type == 'c')
  485.     {
  486.       TREE_LITERAL (t) = 1;
  487.     }
  488.  
  489.   return t;
  490. }
  491.  
  492. /* Return a new node with the same contents as NODE
  493.    except that its TREE_CHAIN is zero and it has a fresh uid.  */
  494.  
  495. tree
  496. copy_node (node)
  497.      tree node;
  498. {
  499.   register tree t;
  500.   register enum tree_code code = TREE_CODE (node);
  501.   register int length;
  502.   register int i;
  503.  
  504.   switch (*tree_code_type[(int) code])
  505.     {
  506.     case 'd':  /* A decl node */
  507.       length = sizeof (struct tree_decl);
  508.       break;
  509.  
  510.     case 't':  /* a type node */
  511.       length = sizeof (struct tree_type);
  512.       break;
  513.  
  514.     case 's':
  515.       length = sizeof (struct tree_common)
  516.     + 2 * sizeof (int)
  517.       + tree_code_length[(int) code] * sizeof (char *);
  518.       break;
  519.  
  520.     case 'r':  /* a reference */
  521.     case 'e':  /* a expression */
  522.       length = sizeof (struct tree_exp)
  523.     + (tree_code_length[(int) code] - 1) * sizeof (char *);
  524.       break;
  525.  
  526.     case 'c':  /* a constant */
  527.       /* We can't use tree_code_length for this, since the number of words
  528.      is machine-dependent due to varying alignment of `double'.  */
  529.       if (code == REAL_CST)
  530.     {
  531.       length = sizeof (struct tree_real_cst);
  532.       break;
  533.     }
  534.  
  535.     case 'x':  /* something random, like an identifier.  */
  536.       length = sizeof (struct tree_common)
  537.     + tree_code_length[(int) code] * sizeof (char *);
  538.     }
  539.  
  540.   t = (tree) obstack_alloc (current_obstack, length);
  541.  
  542.   for (i = ((length + sizeof (int) - 1) / sizeof (int)) - 1;
  543.        i >= 0;
  544.        i--)
  545.     ((int *) t)[i] = ((int *) node)[i];
  546.  
  547.   TREE_UID (t) = tree_node_counter++;
  548.   TREE_CHAIN (t) = 0;
  549.  
  550.   TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
  551.  
  552.   return t;
  553. }
  554.  
  555. /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
  556.    For example, this can copy a list made of TREE_LIST nodes.  */
  557.  
  558. tree
  559. copy_list (list)
  560.      tree list;
  561. {
  562.   tree head;
  563.   register tree prev, next;
  564.  
  565.   if (list == 0)
  566.     return 0;
  567.  
  568.   head = prev = copy_node (list);
  569.   next = TREE_CHAIN (list);
  570.   while (next)
  571.     {
  572.       TREE_CHAIN (prev) = copy_node (next);
  573.       prev = TREE_CHAIN (prev);
  574.       next = TREE_CHAIN (next);
  575.     }
  576.   return head;
  577. }
  578.  
  579. #define HASHBITS 30
  580.  
  581. /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
  582.    If an identifier with that name has previously been referred to,
  583.    the same node is returned this time.  */
  584.  
  585. tree
  586. get_identifier (text)
  587.      register char *text;
  588. {
  589.   register int hi;
  590.   register int i;
  591.   register tree idp;
  592.   register int len, hash_len;
  593.  
  594.   /* Compute length of text in len.  */
  595.   for (len = 0; text[len]; len++);
  596.  
  597.   /* Decide how much of that length to hash on */
  598.   hash_len = len;
  599.   if (warn_id_clash && len > id_clash_len)
  600.     hash_len = id_clash_len;
  601.  
  602.   /* Compute hash code */
  603.   hi = hash_len;
  604.   for (i = 0; i < hash_len; i++)
  605.     hi = ((hi * 613) + (unsigned)(text[i]));
  606.  
  607.   hi &= (1 << HASHBITS) - 1;
  608.   hi %= MAX_HASH_TABLE;
  609.   
  610.   /* Search table for identifier */
  611.   for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
  612.     if (IDENTIFIER_LENGTH (idp) == len
  613.     && !strcmp (IDENTIFIER_POINTER (idp), text))
  614.       return idp;        /* <-- return if found */
  615.   
  616.   /* Not found; optionally warn about a similar identifier */
  617.   if (warn_id_clash && do_identifier_warnings && len > id_clash_len)
  618.     for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
  619.       if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
  620.     {
  621.       warning ("`%s' and `%s' identical in first n characters",
  622.            IDENTIFIER_POINTER (idp), text);
  623.       break;
  624.     }
  625.  
  626.   /* Not found, create one, add to chain */
  627.   idp = make_node (IDENTIFIER_NODE);
  628.   IDENTIFIER_LENGTH (idp) = len;
  629.  
  630.   IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
  631.  
  632.   TREE_CHAIN (idp) = hash_table[hi];
  633.   hash_table[hi] = idp;
  634.   return idp;            /* <-- return if created */
  635. }
  636.  
  637. /* Enable warnings on similar identifiers (if requested).
  638.    Done after the built-in identifiers are created.  */
  639.  
  640. void
  641. start_identifier_warnings ()
  642. {
  643.   do_identifier_warnings = 1;
  644. }
  645.  
  646. /* Record the size of an identifier node for the language in use.
  647.    This is called by the language-specific files.  */
  648.  
  649. void
  650. set_identifier_size (size)
  651.      int size;
  652. {
  653.   tree_code_length[(int) IDENTIFIER_NODE] = size;
  654. }
  655.  
  656. /* Return a newly constructed INTEGER_CST node whose constant value
  657.    is specified by the two ints LOW and HI.
  658.    The TREE_TYPE is set to `int'.  */
  659.  
  660. tree
  661. build_int_2 (low, hi)
  662.      int low, hi;
  663. {
  664.   register tree t = make_node (INTEGER_CST);
  665.   TREE_INT_CST_LOW (t) = low;
  666.   TREE_INT_CST_HIGH (t) = hi;
  667.   TREE_TYPE (t) = integer_type_node;
  668.   return t;
  669. }
  670.  
  671. /* Return a new REAL_CST node whose type is TYPE and value is D.  */
  672.  
  673. tree
  674. build_real (type, d)
  675.      tree type;
  676.      REAL_VALUE_TYPE d;
  677. {
  678.   tree v;
  679.  
  680.   /* Check for valid float value for this type on this target machine;
  681.      if not, can print error message and store a valid value in D.  */
  682. #ifdef CHECK_FLOAT_VALUE
  683.   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
  684. #endif
  685.  
  686.   v = make_node (REAL_CST);
  687.   TREE_TYPE (v) = type;
  688.   TREE_REAL_CST (v) = d;
  689.   return v;
  690. }
  691.  
  692. /* Return a new REAL_CST node whose type is TYPE
  693.    and whose value is the integer value of the INTEGER_CST node I.  */
  694.  
  695. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  696.  
  697. REAL_VALUE_TYPE
  698. real_value_from_int_cst (i)
  699.      tree i;
  700. {
  701.   REAL_VALUE_TYPE d;
  702. #ifdef REAL_ARITHMETIC
  703.   REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
  704. #else /* not REAL_ARITHMETIC */
  705.   if (TREE_INT_CST_HIGH (i) < 0)
  706.     {
  707.       d = (double) (~ TREE_INT_CST_HIGH (i));
  708.       d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  709.         * (double) (1 << (HOST_BITS_PER_INT / 2)));
  710.       d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
  711.       d = (- d - 1.0);
  712.     }
  713.   else
  714.     {
  715.       d = (double) TREE_INT_CST_HIGH (i);
  716.       d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  717.         * (double) (1 << (HOST_BITS_PER_INT / 2)));
  718.       d += (double) (unsigned) TREE_INT_CST_LOW (i);
  719.     }
  720. #endif /* not REAL_ARITHMETIC */
  721.   return d;
  722. }
  723.  
  724. /* This function can't be implemented if we can't do arithmetic
  725.    on the float representation.  */
  726.  
  727. tree
  728. build_real_from_int_cst (type, i)
  729.      tree type;
  730.      tree i;
  731. {
  732.   tree v;
  733.   REAL_VALUE_TYPE d;
  734.  
  735.   v = make_node (REAL_CST);
  736.   TREE_TYPE (v) = type;
  737.  
  738.   d = real_value_from_int_cst (i);
  739.   /* Check for valid float value for this type on this target machine;
  740.      if not, can print error message and store a valid value in D.  */
  741. #ifdef CHECK_FLOAT_VALUE
  742.   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
  743. #endif
  744.  
  745.   TREE_REAL_CST (v) = d;
  746.   return v;
  747. }
  748.  
  749. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  750.  
  751. /* Return a newly constructed STRING_CST node whose value is
  752.    the LEN characters at STR.
  753.    The TREE_TYPE is not initialized.  */
  754.  
  755. tree
  756. build_string (len, str)
  757.      int len;
  758.      char *str;
  759. {
  760.   register tree s = make_node (STRING_CST);
  761.   TREE_STRING_LENGTH (s) = len;
  762.   TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
  763.   return s;
  764. }
  765.  
  766. /* Return a newly constructed COMPLEX_CST node whose value is
  767.    specified by the real and imaginary parts REAL and IMAG.
  768.    Both REAL and IMAG should be constant nodes.
  769.    The TREE_TYPE is not initialized.  */
  770.  
  771. tree
  772. build_complex (real, imag)
  773.      tree real, imag;
  774. {
  775.   register tree t = make_node (COMPLEX_CST);
  776.   TREE_REALPART (t) = real;
  777.   TREE_IMAGPART (t) = imag;
  778.   return t;
  779. }
  780.  
  781. /* Return 1 if EXPR is the integer constant zero.  */
  782.  
  783. int
  784. integer_zerop (expr)
  785.      tree expr;
  786. {
  787.   return (TREE_CODE (expr) == INTEGER_CST
  788.       && TREE_INT_CST_LOW (expr) == 0
  789.       && TREE_INT_CST_HIGH (expr) == 0);
  790. }
  791.  
  792. /* Return 1 if EXPR is the integer constant one.  */
  793.  
  794. int
  795. integer_onep (expr)
  796.      tree expr;
  797. {
  798.   return (TREE_CODE (expr) == INTEGER_CST
  799.       && TREE_INT_CST_LOW (expr) == 1
  800.       && TREE_INT_CST_HIGH (expr) == 0);
  801. }
  802.  
  803. /* Return 1 if EXPR is an integer containing all 1's
  804.    in as much precision as it contains.  */
  805.  
  806. int
  807. integer_all_onesp (expr)
  808.      tree expr;
  809. {
  810.   register int prec;
  811.   register int uns;
  812.  
  813.   if (TREE_CODE (expr) != INTEGER_CST)
  814.     return 0;
  815.  
  816.   uns = TREE_UNSIGNED (TREE_TYPE (expr));
  817.   if (!uns)
  818.     return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
  819.  
  820.   prec = TYPE_PRECISION (TREE_TYPE (expr));
  821.   if (prec >= HOST_BITS_PER_INT)
  822.     return TREE_INT_CST_LOW (expr) == -1
  823.       && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
  824.   else
  825.     return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
  826. }
  827.  
  828. /* Return the length of a chain of nodes chained through TREE_CHAIN.
  829.    We expect a null pointer to mark the end of the chain.
  830.    This is the Lisp primitive `length'.  */
  831.  
  832. int
  833. list_length (t)
  834.      tree t;
  835. {
  836.   register tree tail;
  837.   register int len = 0;
  838.  
  839.   for (tail = t; tail; tail = TREE_CHAIN (tail))
  840.     len++;
  841.  
  842.   return len;
  843. }
  844.  
  845. /* Concatenate two chains of nodes (chained through TREE_CHAIN)
  846.    by modifying the last node in chain 1 to point to chain 2.
  847.    This is the Lisp primitive `nconc'.  */
  848.  
  849. tree
  850. chainon (op1, op2)
  851.      tree op1, op2;
  852. {
  853.   tree t;
  854.  
  855.   if (op1)
  856.     {
  857.       for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
  858.     if (t == op2) abort ();    /* Circularity being created */
  859.       TREE_CHAIN (t) = op2;
  860.       return op1;
  861.     }
  862.   else return op2;
  863. }
  864.  
  865. /* Return a newly created TREE_LIST node whose
  866.    purpose and value fields are PARM and VALUE.  */
  867.  
  868. tree
  869. build_tree_list (parm, value)
  870.      tree parm, value;
  871. {
  872.   register tree t = make_node (TREE_LIST);
  873.   TREE_PURPOSE (t) = parm;
  874.   TREE_VALUE (t) = value;
  875.   return t;
  876. }
  877.  
  878. /* Return a newly created TREE_LIST node whose
  879.    purpose and value fields are PARM and VALUE
  880.    and whose TREE_CHAIN is CHAIN.  */
  881.  
  882. tree
  883. tree_cons (purpose, value, chain)
  884.      tree purpose, value, chain;
  885. {
  886.   register tree node = make_node (TREE_LIST);
  887.   TREE_CHAIN (node) = chain;
  888.   TREE_PURPOSE (node) = purpose;
  889.   TREE_VALUE (node) = value;
  890.   return node;
  891. }
  892.  
  893. /* Same as `tree_cons' but make a permanent object.  */
  894.  
  895. tree
  896. perm_tree_cons (purpose, value, chain)
  897.      tree purpose, value, chain;
  898. {
  899.   register tree node;
  900.   register struct obstack *ambient_obstack = current_obstack;
  901.   current_obstack = &permanent_obstack;
  902.  
  903.   node = make_node (TREE_LIST);
  904.   TREE_CHAIN (node) = chain;
  905.   TREE_PURPOSE (node) = purpose;
  906.   TREE_VALUE (node) = value;
  907.  
  908.   current_obstack = ambient_obstack;
  909.   return node;
  910. }
  911.  
  912. /* Same as `tree_cons', but make this node temporary, regardless.  */
  913.  
  914. tree
  915. temp_tree_cons (purpose, value, chain)
  916.      tree purpose, value, chain;
  917. {
  918.   register tree node;
  919.   register struct obstack *ambient_obstack = current_obstack;
  920.   current_obstack = &temporary_obstack;
  921.  
  922.   node = make_node (TREE_LIST);
  923.   TREE_CHAIN (node) = chain;
  924.   TREE_PURPOSE (node) = purpose;
  925.   TREE_VALUE (node) = value;
  926.  
  927.   current_obstack = ambient_obstack;
  928.   return node;
  929. }
  930.  
  931. /* Same as `tree_cons', but save this node if the function's RTL is saved.  */
  932.  
  933. tree
  934. saveable_tree_cons (purpose, value, chain)
  935.      tree purpose, value, chain;
  936. {
  937.   register tree node;
  938.   register struct obstack *ambient_obstack = current_obstack;
  939.   current_obstack = saveable_obstack;
  940.  
  941.   node = make_node (TREE_LIST);
  942.   TREE_CHAIN (node) = chain;
  943.   TREE_PURPOSE (node) = purpose;
  944.   TREE_VALUE (node) = value;
  945.  
  946.   current_obstack = ambient_obstack;
  947.   return node;
  948. }
  949.  
  950. /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
  951.  
  952. tree
  953. tree_last (chain)
  954.      register tree chain;
  955. {
  956.   register tree next;
  957.   if (chain)
  958.     while (next = TREE_CHAIN (chain))
  959.       chain = next;
  960.   return chain;
  961. }
  962.  
  963. /* Reverse the order of elements in the chain T,
  964.    and return the new head of the chain (old last element).  */
  965.  
  966. tree
  967. nreverse (t)
  968.      tree t;
  969. {
  970.   register tree prev = 0, decl, next;
  971.   for (decl = t; decl; decl = next)
  972.     {
  973.       next = TREE_CHAIN (decl);
  974.       TREE_CHAIN (decl) = prev;
  975.       prev = decl;
  976.     }
  977.   return prev;
  978. }
  979.  
  980. /* Return the size nominally occupied by an object of type TYPE
  981.    when it resides in memory.  The value is measured in units of bytes,
  982.    and its data type is that normally used for type sizes
  983.    (which is the first type created by make_signed_type or
  984.    make_unsigned_type).  */
  985.  
  986. tree
  987. size_in_bytes (type)
  988.      tree type;
  989. {
  990.   if (type == error_mark_node)
  991.     return integer_zero_node;
  992.   type = TYPE_MAIN_VARIANT (type);
  993.   if (TYPE_SIZE (type) == 0)
  994.     {
  995.       incomplete_type_error (0, type);
  996.       return integer_zero_node;
  997.     }
  998.   return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
  999.             BITS_PER_UNIT);
  1000. }
  1001.  
  1002. /* Return the size of TYPE (in bytes) as an integer,
  1003.    or return -1 if the size can vary.  */
  1004.  
  1005. int
  1006. int_size_in_bytes (type)
  1007.      tree type;
  1008. {
  1009.   int size;
  1010.   if (type == error_mark_node)
  1011.     return 0;
  1012.   type = TYPE_MAIN_VARIANT (type);
  1013.   if (TYPE_SIZE (type) == 0)
  1014.     return -1;
  1015.   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
  1016.     return -1;
  1017.   size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  1018.   return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  1019. }
  1020.  
  1021. /* Return, as an INTEGER_CST node, the number of elements for
  1022.    TYPE (which is an ARRAY_TYPE).  */
  1023.  
  1024. tree
  1025. array_type_nelts (type)
  1026.      tree type;
  1027. {
  1028.   tree index_type = TYPE_DOMAIN (type);
  1029.   return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
  1030.       ? TYPE_MAX_VALUE (index_type)
  1031.       : fold (build (MINUS_EXPR, integer_type_node,
  1032.              TYPE_MAX_VALUE (index_type),
  1033.              TYPE_MIN_VALUE (index_type))));
  1034. }
  1035.  
  1036. /* Return nonzero if arg is static -- a reference to an object in
  1037.    static storage.  This is not the same as the C meaning of `static'.  */
  1038.  
  1039. int
  1040. staticp (arg)
  1041.      tree arg;
  1042. {
  1043.   register enum tree_code code = TREE_CODE (arg);
  1044.  
  1045.   if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
  1046.       && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
  1047.     return 1;
  1048.  
  1049.   if (code == STRING_CST)
  1050.     return 1;
  1051.  
  1052.   if (code == COMPONENT_REF)
  1053.     return (DECL_VOFFSET (TREE_OPERAND (arg, 1)) == 0
  1054.         && staticp (TREE_OPERAND (arg, 0)));
  1055.  
  1056.   if (code == INDIRECT_REF)
  1057.     return TREE_LITERAL (TREE_OPERAND (arg, 0));
  1058.  
  1059.   if (code == ARRAY_REF)
  1060.     {
  1061.       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
  1062.       && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
  1063.     return staticp (TREE_OPERAND (arg, 0));
  1064.     }
  1065.  
  1066.   return 0;
  1067. }
  1068.  
  1069. /* Return nonzero if REF is an lvalue valid for this language.
  1070.    Lvalues can be assigned, unless they have TREE_READONLY.
  1071.    Lvalues can have their address taken, unless they have TREE_REGDECL.  */
  1072.  
  1073. int
  1074. lvalue_p (ref)
  1075.      tree ref;
  1076. {
  1077.   register enum tree_code code = TREE_CODE (ref);
  1078.  
  1079.   if (language_lvalue_valid (ref))
  1080.     switch (code)
  1081.       {
  1082.       case COMPONENT_REF:
  1083.     return lvalue_p (TREE_OPERAND (ref, 0));
  1084.  
  1085.       case STRING_CST:
  1086.     return 1;
  1087.  
  1088.       case INDIRECT_REF:
  1089.       case ARRAY_REF:
  1090.       case VAR_DECL:
  1091.       case PARM_DECL:
  1092.       case RESULT_DECL:
  1093.       case ERROR_MARK:
  1094.     if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
  1095.         && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
  1096.       return 1;
  1097.     break;
  1098.  
  1099.       case NEW_EXPR:
  1100.     return 1;
  1101.  
  1102.       case CALL_EXPR:
  1103.     if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
  1104.       return 1;
  1105.       }
  1106.   return 0;
  1107. }
  1108.  
  1109. /* Return nonzero if REF is an lvalue valid for this language;
  1110.    otherwise, print an error message and return zero.  */
  1111.  
  1112. int
  1113. lvalue_or_else (ref, string)
  1114.      tree ref;
  1115.      char *string;
  1116. {
  1117.   int win = lvalue_p (ref);
  1118.   if (! win)
  1119.     error ("invalid lvalue in %s", string);
  1120.   return win;
  1121. }
  1122.  
  1123. /* This should be applied to any node which may be used in more than one place,
  1124.    but must be evaluated only once.  Normally, the code generator would
  1125.    reevaluate the node each time; this forces it to compute it once and save
  1126.    the result.  This is done by encapsulating the node in a SAVE_EXPR.  */
  1127.  
  1128. tree
  1129. save_expr (expr)
  1130.      tree expr;
  1131. {
  1132.   register tree t = fold (expr);
  1133.  
  1134.   /* If the tree evaluates to a constant, then we don't want to hide that
  1135.      fact (i.e. this allows further folding, and direct checks for constants).
  1136.      Since it is no problem to reevaluate literals, we just return the 
  1137.      literal node. */
  1138.  
  1139.   if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
  1140.     return t;
  1141.  
  1142.   return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
  1143. }
  1144.  
  1145. /* Stabilize a reference so that we can use it any number of times
  1146.    without causing its operands to be evaluated more than once.
  1147.    Returns the stabilized reference.
  1148.  
  1149.    Also allows conversion expressions whose operands are references.
  1150.    Any other kind of expression is returned unchanged.  */
  1151.  
  1152. tree
  1153. stabilize_reference (ref)
  1154.      tree ref;
  1155. {
  1156.   register tree result;
  1157.   register enum tree_code code = TREE_CODE (ref);
  1158.  
  1159.   switch (code)
  1160.     {
  1161.     case VAR_DECL:
  1162.     case PARM_DECL:
  1163.     case RESULT_DECL:
  1164.       result = ref;
  1165.       break;
  1166.  
  1167.     case NOP_EXPR:
  1168.     case CONVERT_EXPR:
  1169.     case FLOAT_EXPR:
  1170.     case FIX_TRUNC_EXPR:
  1171.     case FIX_FLOOR_EXPR:
  1172.     case FIX_ROUND_EXPR:
  1173.     case FIX_CEIL_EXPR:
  1174.       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
  1175.       break;
  1176.  
  1177.     case INDIRECT_REF:
  1178.       result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
  1179.       break;
  1180.  
  1181.     case COMPONENT_REF:
  1182.       result = build_nt (COMPONENT_REF,
  1183.              stabilize_reference (TREE_OPERAND (ref, 0)),
  1184.              TREE_OPERAND (ref, 1));
  1185.       break;
  1186.  
  1187.     case ARRAY_REF:
  1188.       result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
  1189.              save_expr (TREE_OPERAND (ref, 1)));
  1190.       break;
  1191.  
  1192.       /* If arg isn't a kind of lvalue we recognize, make no change.
  1193.      Caller should recognize the error for an invalid lvalue.  */
  1194.     default:
  1195.       return ref;
  1196.  
  1197.     case ERROR_MARK:
  1198.       return error_mark_node;
  1199.     }
  1200.  
  1201.   TREE_TYPE (result) = TREE_TYPE (ref);
  1202.   TREE_READONLY (result) = TREE_READONLY (ref);
  1203.   TREE_VOLATILE (result) = TREE_VOLATILE (ref);
  1204.   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
  1205.  
  1206.   return result;
  1207. }
  1208.  
  1209. /* Low-level constructors for expressions.  */
  1210.  
  1211. /* Build an expression of code CODE, data type TYPE,
  1212.    and operands as specified by the arguments ARG1 and following arguments.
  1213.    Expressions and reference nodes can be created this way.
  1214.    Constants, decls, types and misc nodes cannot be.  */
  1215.  
  1216. tree
  1217. #ifdef APPLE_HAX_VA
  1218. build (enum tree_code code, ...)
  1219. {
  1220.   register va_list p;
  1221. #else
  1222. build (va_alist)
  1223.      va_dcl
  1224. {
  1225.   register va_list p;
  1226.   enum tree_code code;
  1227. #endif /* APPLE_HAX */
  1228.   register tree t;
  1229.   register int length;
  1230.   register int i;
  1231.  
  1232. #ifdef APPLE_HAX_VA
  1233.   va_start (p, code);
  1234. #else
  1235.   va_start (p);
  1236.  
  1237.   code = va_arg (p, enum tree_code);
  1238. #endif /* APPLE_HAX */
  1239.   t = make_node (code);
  1240.   length = tree_code_length[(int) code];
  1241.   TREE_TYPE (t) = va_arg (p, tree);
  1242.  
  1243.   if (length == 2)
  1244.     {
  1245.       /* This is equivalent to the loop below, but faster.  */
  1246.       register tree arg0 = va_arg (p, tree);
  1247.       register tree arg1 = va_arg (p, tree);
  1248.       TREE_OPERAND (t, 0) = arg0;
  1249.       TREE_OPERAND (t, 1) = arg1;
  1250.       TREE_VOLATILE (t)
  1251.     = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
  1252.     }
  1253.   else
  1254.     {
  1255.       for (i = 0; i < length; i++)
  1256.     {
  1257.       register tree operand = va_arg (p, tree);
  1258.       TREE_OPERAND (t, i) = operand;
  1259.       if (operand && TREE_VOLATILE (operand))
  1260.         TREE_VOLATILE (t) = 1;
  1261.     }
  1262.     }
  1263.   va_end (p);
  1264.   return t;
  1265. }
  1266.  
  1267. /* Similar except don't specify the TREE_TYPE
  1268.    and leave the TREE_VOLATILE as 0.
  1269.    It is permissible for arguments to be null,
  1270.    or even garbage if their values do not matter.  */
  1271.  
  1272. tree
  1273. #ifdef APPLE_HAXe;
  1274.      int line;
  1275.      tree body;
  1276. {
  1277.   register tree t = make_node (LOOP_STMT);
  1278.   STMT_SOURCE_FILE (t) = filename;
  1279.   STMT_SOURCE_LINE (t) = line;
  1280.   STMT_BODY (t) = body;
  1281.   return t;
  1282. }
  1283.  
  1284. tree
  1285. build_compound (filename, line, body)
  1286.      char *filename;
  1287.      int line;
  1288.      tree body;
  1289. {
  1290.   register tree t = make_node (COMPOUND_STMT);
  1291.   STMT_SOURCE_FILE (t) = filename;
  1292.   STMT_SOURCE_LINE (t) = line;
  1293.   STMT_BODY (t) = body;
  1294.   return t;
  1295. }
  1296.  
  1297. #endif /* 0 */
  1298.  
  1299. /* LET_STMT nodes are used to represent the structure of binding contours
  1300.    and declarations, once those contours have been exited and their contents
  1301.    compiled.  This information is used for outputting debugging info.  */
  1302.  
  1303. tree
  1304. build_let (filename, line, vars, subblocks, supercontext, tags)
  1305.      char *filename;
  1306.      int line;
  1307.      tree vars, subblocks, supercontext, tags;
  1308. {
  1309.   register tree t = make_node (LET_STMT);
  1310.   STMT_SOURCE_FILE (t) = filename;
  1311.   STMT_SOURCE_LINE (t) = line;
  1312.   STMT_VARS (t) = vars;
  1313.   STMT_SUBBLOCKS (t) = subblocks;
  1314.   STMT_SUPERCONTEXT (t) = supercontext;
  1315.   STMT_BIND_SIZE (t) = 0;
  1316.   STMT_TYPE_TAGS (t) = tags;
  1317.   return t;
  1318. }
  1319.  
  1320. /* Return a type like TYPE except that its TREE_READONLY is CONSTP
  1321.    and its TREE_VOLATILE is VOLATILEP.
  1322.  
  1323.    Such variant types already made are recorded so that duplicates
  1324.    are not made.
  1325.  
  1326.    A variant types should never be used as the type of an expression.
  1327.    Always copy the variant information into the TREE_READONLY
  1328.    and TREE_VOLATILE of the expression, and then give the expression
  1329.    as its type the "main variant", the variant whose TREE_READONLY
  1330.    and TREE_VOLATILE are zero.  Use TYPE_MAIN_VARIANT to find the
  1331.    main variant.  */
  1332.  
  1333. tree
  1334. build_type_variant (type, constp, volatilep, pascalp)
  1335.      tree type;
  1336.      int constp, volatilep, pascalp;
  1337. {
  1338.   register tree t, m = TYPE_MAIN_VARIANT (type);
  1339.   register struct obstack *ambient_obstack = current_obstack;
  1340.  
  1341.   /* Treat any nonzero argument as 1.  */
  1342.   constp = !!constp;
  1343.   volatilep = !!volatilep;
  1344.   pascalp = !!pascalp;
  1345.  
  1346.   /* First search the chain variants for one that is what we want.  */
  1347.  
  1348.   for (t = m; t; t = TYPE_NEXT_VARIANT (t))
  1349.     if (constp == TREE_READONLY (t)
  1350.     && volatilep == TREE_VOLATILE (t) && pascalp == TREE_PASCAL (t))
  1351.       return t;
  1352.  
  1353.   /* We need a new one.  */
  1354.   current_obstack
  1355.     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1356.  
  1357.   t = copy_node (type);
  1358.   TREE_READONLY (t) = constp;
  1359.   TREE_VOLATILE (t) = volatilep;
  1360.   TREE_PASCAL (t) = pascalp;
  1361.   TYPE_POINTER_TO (t) = 0;
  1362.   TYPE_REFERENCE_TO (t) = 0;
  1363.  
  1364.   /* Add this type to the chain of variants of TYPE.  */
  1365.   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  1366.   TYPE_NEXT_VARIANT (m) = t;
  1367.  
  1368.   current_obstack = ambient_obstack;
  1369.   return t;
  1370. }
  1371.  
  1372. /* Hashing of types so that we don't make duplicates.
  1373.    The entry point is `type_hash_canon'.  */
  1374.  
  1375. /* Each hash table slot is a bucket containing a chain
  1376.    of these structures.  */
  1377.  
  1378. struct type_hash
  1379. {
  1380.   struct type_hash *next;    /* Next structure in the bucket.  */
  1381.   int hashcode;            /* Hash code of this type.  */
  1382.   tree type;            /* The type recorded here.  */
  1383. };
  1384.  
  1385. /* Now here is the hash table.  When recording a type, it is added
  1386.    to the slot whose index is the hash code mod the table size.
  1387.    Note that the hash table is used for several kinds of types
  1388.    (function types, array types and array index range types, for now).
  1389.    While all these live in the same table, they are completely independent,
  1390.    and the hash code is computed differently for each of these.  */
  1391.  
  1392. #define TYPE_HASH_SIZE 59
  1393. struct type_hash *type_hash_table[TYPE_HASH_SIZE];
  1394.  
  1395. #ifdef APPLE_HAX
  1396.  
  1397. void clear_type_hash_table()
  1398.     {
  1399.     bzero(type_hash_table, sizeof type_hash_table);
  1400.     }
  1401.  
  1402. #endif
  1403.  
  1404. /* Here is how primitive or already-canonicalized types' hash
  1405.    codes are made.  */
  1406. #define TYPE_HASH(TYPE) TREE_UID (TYPE)
  1407.  
  1408. /* Compute a hash code for a list of types (chain of TREE_LIST nodes
  1409.    with types in the TREE_VALUE slots), by adding the hash codes
  1410.    of the individual types.  */
  1411.  
  1412. int
  1413. type_hash_list (list)
  1414.      tree list;
  1415. {
  1416.   register int hashcode;
  1417.   register tree tail;
  1418.   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
  1419.     hashcode += TYPE_HASH (TREE_VALUE (tail));
  1420.   return hashcode;
  1421. }
  1422.  
  1423. /* Look in the type hash table for a type isomorphic to TYPE.
  1424.    If one is found, return it.  Otherwise return 0.  */
  1425.  
  1426. tree
  1427. type_hash_lookup (hashcode, type)
  1428.      int hashcode;
  1429.      tree type;
  1430. {
  1431.   register struct type_hash *h;
  1432.   for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  1433.     if (h->hashcode == hashcode
  1434.     && TREE_CODE (h->type) == TREE_CODE (type)
  1435.     && TREE_TYPE (h->type) == TREE_TYPE (type)
  1436.     && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
  1437.         || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
  1438.                    TYPE_MAX_VALUE (type)))
  1439.     && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
  1440.         || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
  1441.                    TYPE_MIN_VALUE (type)))
  1442.     && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
  1443.         || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
  1444.         && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
  1445.         && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
  1446.       return h->type;
  1447.   return 0;
  1448. }
  1449.  
  1450. /* Add an entry to the type-hash-table
  1451.    for a type TYPE whose hash code is HASHCODE.  */
  1452.  
  1453. void
  1454. type_hash_add (hashcode, type)
  1455.      int hashcode;
  1456.      tree type;
  1457. {
  1458.   register struct type_hash *h;
  1459.  
  1460.   h = (struct type_hash *) oballoc (sizeof (struct type_hash));
  1461.   h->hashcode = hashcode;
  1462.   h->type = type;
  1463.   h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
  1464.   type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
  1465. }
  1466.  
  1467. /* Given TYPE, and HASHCODE its hash code, return the canonical
  1468.    object for an identical type if one already exists.
  1469.    Otherwise, return TYPE, and record it as the canonical object
  1470.    if it is a permanent object.
  1471.  
  1472.    To use this function, first create a type of the sort you want.
  1473.    Then compute its hash code from the fields of the type that
  1474.    make it different from other similar types.
  1475.    Then call this function and use the value.
  1476.    This function frees the type you pass in if it is a duplicate.  */
  1477.  
  1478. /* Set to 1 to debug without canonicalization.  Never set by program.  */
  1479. #ifdef APPLE_HAX
  1480. int debug_no_type_hash =
  1481. #ifdef __GNUC__
  1482. 1
  1483. #else
  1484. 0
  1485. #endif
  1486. ;
  1487. #else
  1488. int debug_no_type_hash = 0;
  1489. #endif /* APPLE_HAX */
  1490.  
  1491. tree
  1492. type_hash_canon (hashcode, type)
  1493.      int hashcode;
  1494.      tree type;
  1495. {
  1496.   tree t1;
  1497.  
  1498.   if (debug_no_type_hash)
  1499.     return type;
  1500.  
  1501.   t1 = type_hash_lookup (hashcode, type);
  1502.   if (t1 != 0)
  1503.     {
  1504.       struct obstack *o
  1505.     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1506.       obstack_free (o, type);
  1507.       return t1;
  1508.     }
  1509.  
  1510.   /* If this is a new type, record it for later reuse.  */
  1511.   if (current_obstack == &permanent_obstack)
  1512.     type_hash_add (hashcode, type);
  1513.  
  1514.   return type;
  1515. }
  1516.  
  1517. /* Given two lists of types
  1518.    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
  1519.    return 1 if the lists contain the same types in the same order.
  1520.    Also, the TREE_PURPOSEs must match.  */
  1521.  
  1522. int
  1523. type_list_equal (l1, l2)
  1524.      tree l1, l2;
  1525. {
  1526.   register tree t1, t2;
  1527.   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
  1528.     {
  1529.       if (TREE_VALUE (t1) != TREE_VALUE (t2))
  1530.     return 0;
  1531.       if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
  1532.       && !simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
  1533.     return 0;
  1534.     }
  1535.  
  1536.   return t1 == t2;
  1537. }
  1538.  
  1539. /* Nonzero if integer constants T1 and T2
  1540.    represent the same constant value.  */
  1541.  
  1542. int
  1543. tree_int_cst_equal (t1, t2)
  1544.      tree t1, t2;
  1545. {
  1546.   if (t1 == t2)
  1547.     return 1;
  1548.   if (t1 == 0 || t2 == 0)
  1549.     return 0;
  1550.   if (TREE_CODE (t1) == INTEGER_CST
  1551.       && TREE_CODE (t2) == INTEGER_CST
  1552.       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
  1553.       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
  1554.     return 1;
  1555.   return 0;
  1556. }
  1557.  
  1558. /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
  1559.    The precise way of comparison depends on their data type.  */
  1560.  
  1561. int
  1562. tree_int_cst_lt (t1, t2)
  1563.      tree t1, t2;
  1564. {
  1565.   if (t1 == t2)
  1566.     return 0;
  1567.  
  1568.   if (!TREE_UNSIGNED (TREE_TYPE (t1)))
  1569.     return INT_CST_LT (t1, t2);
  1570.   return INT_CST_LT_UNSIGNED (t1, t2);
  1571. }
  1572.  
  1573. /* Compare two constructor-element-type constants.  */
  1574.  
  1575. int
  1576. simple_cst_equal (t1, t2)
  1577.      tree t1, t2;
  1578. {
  1579.   register enum tree_code code1, code2;
  1580.  
  1581.   if (t1 == t2)
  1582.     return 1;
  1583.   if (t1 == 0 || t2 == 0)
  1584.     return 0;
  1585.  
  1586.   code1 = TREE_CODE (t1);
  1587.   code2 = TREE_CODE (t2);
  1588.  
  1589.   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
  1590.     if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
  1591.       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
  1592.     else
  1593.       return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
  1594.   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
  1595.     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
  1596.  
  1597.   if (code1 != code2)
  1598.     return 0;
  1599.  
  1600.   switch (code1)
  1601.     {
  1602.     case INTEGER_CST:
  1603.       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
  1604.     && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
  1605.  
  1606.     case REAL_CST:
  1607.       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
  1608.  
  1609.     case STRING_CST:
  1610.       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
  1611.     && !strcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2));
  1612.  
  1613.     case CONSTRUCTOR:
  1614.       abort ();
  1615.  
  1616.     case VAR_DECL:
  1617.     case PARM_DECL:
  1618.     case CONST_DECL:
  1619.       return 0;
  1620.  
  1621.     case PLUS_EXPR:
  1622.     case MINUS_EXPR:
  1623.     case MULT_EXPR:
  1624.     case TRUNC_DIV_EXPR:
  1625.     case TRUNC_MOD_EXPR:
  1626.     case LSHIFT_EXPR:
  1627.     case RSHIFT_EXPR:
  1628.       return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
  1629.           && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
  1630.  
  1631.     case NEGATE_EXPR:
  1632.     case ADDR_EXPR:
  1633.     case REFERENCE_EXPR:
  1634.     case INDIRECT_REF:
  1635.       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
  1636.  
  1637.     default:
  1638.       abort ();
  1639.     }
  1640. }
  1641.  
  1642. /* Constructors for pointer, array and function types.
  1643.    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
  1644.    constructed by language-dependent code, not here.)  */
  1645.  
  1646. /* Construct, lay out and return the type of pointers to TO_TYPE.
  1647.    If such a type has already been constructed, reuse it.  */
  1648.  
  1649. tree
  1650. build_pointer_type (to_type)
  1651.      tree to_type;
  1652. {
  1653.   register tree t = TYPE_POINTER_TO (to_type);
  1654.   register struct obstack *ambient_obstack = current_obstack;
  1655.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1656.  
  1657.   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
  1658.  
  1659.   if (t)
  1660.     return t;
  1661.  
  1662.   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
  1663.   if (TREE_PERMANENT (to_type))
  1664.     {
  1665.       current_obstack = &permanent_obstack;
  1666.       saveable_obstack = &permanent_obstack;
  1667.     }
  1668.  
  1669.   t = make_node (POINTER_TYPE);
  1670.   TREE_TYPE (t) = to_type;
  1671.  
  1672.   /* Record this type as the pointer to TO_TYPE.  */
  1673.   TYPE_POINTER_TO (to_type) = t;
  1674.  
  1675.   /* Lay out the type.  This function has many callers that are concerned
  1676.      with expression-construction, and this simplifies them all.
  1677.      Also, it guarantees the TYPE_SIZE is permanent if the type is.  */
  1678.   layout_type (t);
  1679.  
  1680.   current_obstack = ambient_obstack;
  1681.   saveable_obstack = ambient_saveable_obstack;
  1682.   return t;
  1683. }
  1684.  
  1685. /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
  1686.    MAXVAL should be the maximum value in the domain
  1687.    (one less than the length of the array).  */
  1688.  
  1689. tree
  1690. build_index_type (maxval)
  1691.      tree maxval;
  1692. {
  1693.   register tree itype = make_node (INTEGER_TYPE);
  1694.   int maxint = TREE_INT_CST_LOW (maxval);
  1695.   TYPE_PRECISION (itype) = BITS_PER_WORD;
  1696.   TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
  1697.   TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
  1698.   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
  1699.   TYPE_MODE (itype) = SImode;
  1700.   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
  1701.   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
  1702.   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
  1703.   return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
  1704. }
  1705.  
  1706. /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
  1707.    and number of elements specified by the range of values of INDEX_TYPE.
  1708.    If such a type has already been constructed, reuse it.  */
  1709.  
  1710. tree
  1711. build_array_type (elt_type, index_type)
  1712.      tree elt_type, index_type;
  1713. {
  1714.   register tree t = make_node (ARRAY_TYPE);
  1715.   int hashcode;
  1716.  
  1717.   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
  1718.     {
  1719.       error ("arrays of functions are not meaningful");
  1720.       elt_type = integer_type_node;
  1721.     }
  1722.  
  1723.   TREE_TYPE (t) = elt_type;
  1724.   TYPE_DOMAIN (t) = index_type;
  1725.  
  1726.   /* Make sure TYPE_POINTER_TO (elt_type) is filled in.  */
  1727.   build_pointer_type (elt_type);
  1728.  
  1729.   if (index_type == 0)
  1730.     return t;
  1731.  
  1732.   hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
  1733.   t = type_hash_canon (hashcode, t);
  1734.  
  1735.   if (TYPE_SIZE (t) == 0)
  1736.     layout_type (t);
  1737.   return t;
  1738. }
  1739.  
  1740. /* Construct, lay out and return
  1741.    the type of functions returning type VALUE_TYPE
  1742.    given arguments of types ARG_TYPES.
  1743.    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
  1744.    are data type nodes for the arguments of the function.
  1745.    If such a type has already been constructed, reuse it.  */
  1746.  
  1747. tree
  1748. build_function_type (value_type, arg_types)
  1749.      tree value_type, arg_types;
  1750. {
  1751.   register tree t;
  1752.   int hashcode;
  1753. #if 0 /* def APPLE_C */
  1754.   extern int is_pascal_function_type;
  1755. #endif
  1756.  
  1757.   if (TREE_CODE (value_type) == FUNCTION_TYPE
  1758.       || TREE_CODE (value_type) == ARRAY_TYPE)
  1759.     {
  1760.       error ("function return type cannot be function or array");
  1761.       value_type = integer_type_node;
  1762.     }
  1763.  
  1764.   /* Make a node of the sort we want.  */
  1765.   t = make_node (FUNCTION_TYPE);
  1766.   TREE_TYPE (t) = value_type;
  1767.   TYPE_ARG_TYPES (t) = arg_types;
  1768.  
  1769.   /* If we already have such a type, use the old one and free this one.  */
  1770.   hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
  1771.   t = type_hash_canon (hashcode, t);
  1772.  
  1773.   if (TYPE_SIZE (t) == 0)
  1774.     layout_type (t);
  1775.   return t;
  1776. }
  1777.  
  1778. /* Build the node for the type of references-to-TO_TYPE.  */
  1779.  
  1780. tree
  1781. build_reference_type (to_type)
  1782.      tree to_type;
  1783. {
  1784.   register tree t = TYPE_REFERENCE_TO (to_type);
  1785.   register struct obstack *ambient_obstack = current_obstack;
  1786.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1787.  
  1788.   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
  1789.  
  1790.   if (t)
  1791.     return t;
  1792.  
  1793.   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
  1794.   if (TREE_PERMANENT (to_type))
  1795.     {
  1796.       current_obstack = &permanent_obstack;
  1797.       saveable_obstack = &permanent_obstack;
  1798.     }
  1799.  
  1800.   t = make_node (REFERENCE_TYPE);
  1801.   TREE_TYPE (t) = to_type;
  1802.  
  1803.   /* Record this type as the pointer to TO_TYPE.  */
  1804.   TYPE_REFERENCE_TO (to_type) = t;
  1805.  
  1806.   layout_type (t);
  1807.  
  1808.   current_obstack = ambient_obstack;
  1809.   saveable_obstack = ambient_saveable_obstack;
  1810.   return t;
  1811. }
  1812.  
  1813. /* Construct, lay out and return the type of methods belonging to class
  1814.    BASETYPE and whose arguments and values are described by TYPE.
  1815.    If that type exists already, reuse it.
  1816.    TYPE must be a FUNCTION_TYPE node.  */
  1817.  
  1818. tree
  1819. build_method_type (basetype, type)
  1820.      tree basetype, type;
  1821. {
  1822.   register tree t;
  1823.   int hashcode;
  1824.  
  1825.   /* Make a node of the sort we want.  */
  1826.   t = make_node (METHOD_TYPE);
  1827.  
  1828.   if (TREE_CODE (type) != FUNCTION_TYPE)
  1829.     abort ();
  1830.  
  1831.   TYPE_METHOD_BASETYPE (t) = basetype;
  1832.   TREE_TYPE (t) = TREE_TYPE (type);
  1833.  
  1834.   /* The actual arglist for this function includes a "hidden" argument
  1835.      which is "this".  Put it into the list of argument types.  */
  1836.  
  1837.   TYPE_ARG_TYPES (t)
  1838.     = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
  1839.  
  1840.   /* If we already have such a type, use the old one and free this one.  */
  1841.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
  1842.   t = type_hash_canon (hashcode, t);
  1843.  
  1844.   if (TYPE_SIZE (t) == 0)
  1845.     layout_type (t);
  1846.  
  1847.   return t;
  1848. }
  1849.  
  1850. /* Construct, lay out and return the type of methods belonging to class
  1851.    BASETYPE and whose arguments and values are described by TYPE.
  1852.    If that type exists already, reuse it.
  1853.    TYPE must be a FUNCTION_TYPE node.  */
  1854.  
  1855. tree
  1856. build_offset_type (basetype, type)
  1857.      tree basetype, type;
  1858. {
  1859.   register tree t;
  1860.   int hashcode;
  1861.  
  1862.   /* Make a node of the sort we want.  */
  1863.   t = make_node (OFFSET_TYPE);
  1864.  
  1865.   TYPE_OFFSET_BASETYPE (t) = basetype;
  1866.   TREE_TYPE (t) = type;
  1867.  
  1868.   /* If we already have such a type, use the old one and free this one.  */
  1869.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
  1870.   t = type_hash_canon (hashcode, t);
  1871.  
  1872.   if (TYPE_SIZE (t) == 0)
  1873.     layout_type (t);
  1874.  
  1875.   return t;
  1876. }
  1877.  
  1878. /* Return OP, stripped of any conversions to wider types as much as is safe.
  1879.    Converting the value back to OP's type makes a value equivalent to OP.
  1880.  
  1881.    If FOR_TYPE is nonzero, we return a value which, if converted to
  1882.    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
  1883.  
  1884.    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
  1885.    narrowest type that can hold the value, even if they don't exactly fit.
  1886.    Otherwise, bit-field references are changed to a narrower type
  1887.    only if they can be fetched directly from memory in that type.
  1888.  
  1889.    OP must have integer, real or enumeral type.  Pointers are not allowed!
  1890.  
  1891.    There are some cases where the obvious value we could return
  1892.    would regenerate to OP if converted to OP's type, 
  1893.    but would not extend like OP to wider types.
  1894.    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
  1895.    For example, if OP is (unsigned short)(signed char)-1,
  1896.    we avoid returning (signed char)-1 if FOR_TYPE is int,
  1897.    even though extending that to an unsigned short would regenerate OP,
  1898.    since the result of extending (signed char)-1 to (int)
  1899.    is different from (int) OP.  */
  1900.  
  1901. tree
  1902. get_unwidened (op, for_type)
  1903.      register tree op;
  1904.      tree for_type;
  1905. {
  1906.   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
  1907.   /* TYPE_PRECISION is safe in place of type_precision since
  1908.      pointer types are not allowed.  */
  1909.   register tree type = TREE_TYPE (op);
  1910.   register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
  1911.   register int uns
  1912.     = (for_type != 0 && for_type != type
  1913.        && final_prec > TYPE_PRECISION (type)
  1914.        && TREE_UNSIGNED (type));
  1915.   register tree win = op;
  1916.  
  1917.   while (TREE_CODE (op) == NOP_EXPR)
  1918.     {
  1919.       register int bitschange
  1920.     = TYPE_PRECISION (TREE_TYPE (op))
  1921.       - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  1922.  
  1923.       /* Truncations are many-one so cannot be removed.
  1924.      Unless we are later going to truncate down even farther.  */
  1925.       if (bitschange < 0
  1926.       && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
  1927.     break;
  1928.  
  1929.       /* See what's inside this conversion.  If we decide to strip it,
  1930.      we will set WIN.  */
  1931.       op = TREE_OPERAND (op, 0);
  1932.  
  1933.       /* If we have not stripped any zero-extensions (uns is 0),
  1934.      we can strip any kind of extension.
  1935.      If we have previously stripped a zero-extension,
  1936.      only zero-extensions can safely be stripped.
  1937.      Any extension can be stripped if the bits it would produce
  1938.      are all going to be discarded later by truncating to FOR_TYPE.  */
  1939.  
  1940.       if (bitschange > 0)
  1941.     {
  1942.       if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
  1943.         win = op;
  1944.       /* TREE_UNSIGNED says whether this is a zero-extension.
  1945.          Let's avoid computing it if it does not affect WIN
  1946.          and if UNS will not be needed again.  */
  1947.       if ((uns || TREE_CODE (op) == NOP_EXPR)
  1948.           && TREE_UNSIGNED (TREE_TYPE (op)))
  1949.         {
  1950.           uns = 1;
  1951.           win = op;
  1952.         }
  1953.     }
  1954.     }
  1955.  
  1956.   if (TREE_CODE (op) == COMPONENT_REF
  1957.       /* Since type_for_size always gives an integer type.  */
  1958.       && TREE_CODE (type) != REAL_TYPE)
  1959.     {
  1960.       int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  1961.                * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  1962.       type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
  1963.  
  1964.       /* We can get this structure field in the narrowest type it fits in.
  1965.      If FOR_TYPE is 0, do this only for a field that matches the
  1966.      narrower type exactly and is aligned for it (i.e. mode isn't BI).
  1967.      The resulting extension to its nominal type (a fullword type)
  1968.      must fit the same conditions as for other extensions.  */
  1969.  
  1970.       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  1971.       && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
  1972.       && (! uns || final_prec <= innerprec
  1973.           || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  1974.       && type != 0)
  1975.     {
  1976.       win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  1977.                TREE_OPERAND (op, 1));
  1978.       TREE_VOLATILE (win) = TREE_VOLATILE (op);
  1979.       TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  1980.     }
  1981.     }
  1982.   return win;
  1983. }
  1984.  
  1985. /* Return OP or a simpler expression for a narrower value
  1986.    which can be sign-extended or zero-extended to give back OP.
  1987.    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
  1988.    or 0 if the value should be sign-extended.  */
  1989.  
  1990. tree
  1991. get_narrower (op, unsignedp_ptr)
  1992.      register tree op;
  1993.      int *unsignedp_ptr;
  1994. {
  1995.   register int uns = 0;
  1996.   int first = 1;
  1997.   register tree win = op;
  1998.  
  1999.   while (TREE_CODE (op) == NOP_EXPR)
  2000.     {
  2001.       register int bitschange
  2002.     = TYPE_PRECISION (TREE_TYPE (op))
  2003.       - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  2004.  
  2005.       /* Truncations are many-one so cannot be removed.  */
  2006.       if (bitschange < 0)
  2007.     break;
  2008.  
  2009.       /* See what's inside this conversion.  If we decide to strip it,
  2010.      we will set WIN.  */
  2011.       op = TREE_OPERAND (op, 0);
  2012.  
  2013.       if (bitschange > 0)
  2014.     {
  2015.       /* An extension: the outermost one can be stripped,
  2016.          but remember whether it is zero or sign extension.  */
  2017.       if (first)
  2018.         uns = TREE_UNSIGNED (TREE_TYPE (op));
  2019.       /* Otherwise, if a sign extension has been stripped,
  2020.          only sign extensions can now be stripped;
  2021.          if a zero extension has been stripped, only zero-extensions.  */
  2022.       else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
  2023.         break;
  2024.       first = 0;
  2025.     }
  2026.       /* A change in nominal type can always be stripped.  */
  2027.  
  2028.       win = op;
  2029.     }
  2030.  
  2031.   if (TREE_CODE (op) == COMPONENT_REF
  2032.       /* Since type_for_size always gives an integer type.  */
  2033.       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
  2034.     {
  2035.       int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  2036.                * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  2037.       tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
  2038.  
  2039.       /* We can get this structure field in a narrower type that fits it,
  2040.      but the resulting extension to its nominal type (a fullword type)
  2041.      must satisfy the same conditions as for other extensions.
  2042.  
  2043.      Do this only for fields that are aligned (not BImode),
  2044.      because when bit-field insns will be used there is no
  2045.      advantage in doing this.  */
  2046.  
  2047.       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  2048.       && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
  2049.       && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  2050.       && type != 0)
  2051.     {
  2052.       if (first)
  2053.         uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
  2054.       win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  2055.                TREE_OPERAND (op, 1));
  2056.       TREE_VOLATILE (win) = TREE_VOLATILE (op);
  2057.       TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  2058.     }
  2059.     }
  2060.   *unsignedp_ptr = uns;
  2061.   return win;
  2062. }
  2063.  
  2064. /* Return the precision of a type, for arithmetic purposes.
  2065.    Supports all types on which arithmetic is possible
  2066.    (including pointer types).
  2067.    It's not clear yet what will be right for complex types.  */
  2068.  
  2069. int
  2070. type_precision (type)
  2071.      register tree type;
  2072. {
  2073.   return ((TREE_CODE (type) == INTEGER_TYPE
  2074.        || TREE_CODE (type) == ENUMERAL_TYPE
  2075.        || TREE_CODE (type) == REAL_TYPE)
  2076.       ? TYPE_PRECISION (type) : POINTER_SIZE);
  2077. }
  2078.  
  2079. /* Nonzero if integer constant C has a value that is permissible
  2080.    for type TYPE (an INTEGER_TYPE).  */
  2081.  
  2082. int
  2083. int_fits_type_p (c, type)
  2084.      tree c, type;
  2085. {
  2086.   if (TREE_UNSIGNED (type))
  2087.     return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
  2088.         && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
  2089.   else
  2090.     return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
  2091.         && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
  2092. }
  2093.  
  2094. init_tree2()
  2095. {
  2096. #define INIT_TREE
  2097. #include "init.c"
  2098. }
  2099.